package com.gxgy.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gxgy.base.BaseAllInterface;
import com.gxgy.entity.User;
import com.gxgy.uilt.MD5Util;

@Controller
public class LoginController extends BaseAllInterface{

	@RequestMapping("/login")
	public String login() {
		return "page/login";
	}
	
	@RequestMapping("/doLogin")
	public String doLogin(@RequestParam("username") String username,
			@RequestBody @RequestParam("password") String password,Model model,HttpSession session) {
		System.out.println("==============="+username);
		System.out.println("=============="+password);
		User user = userService.queryUserByUserName(username);
		
		if (username.isEmpty()) {
			model.addAttribute("msg","用户名不能为空");
			return "page/login";
			
		}
		if (password.isEmpty()) {
			model.addAttribute("msg","密码不能为空");
			return "page/login";
		}

		if (user != null) {
			model.addAttribute("user",user);
			
			if (user.getPassword().equals(MD5Util.md5Encryption(password))) {
				session.setAttribute("loginUser", user.getName());
				return "redirect:index";
			}
		}
			model.addAttribute("msg", "用户名或密码错误");
			return "page/login";
		
	}
	
	@RequestMapping("/exitLogin")
	public String exitLogin(HttpServletRequest req, HttpServletResponse resp) {
		HttpSession session = req.getSession();
		session.invalidate();
		return "page/login";
		
	}
}
